/**
 * Filters out valid string values from an array based on a set of allowed enum values.
 *
 * This function takes an array of allowed enum values and an array of values to be filtered.
 * It filters out the values that are strings and exist within the enum values array.
 * Any value that is not a string or does not exist in the enum values array will be excluded.
 *
 * @param enumValues - An array of allowed enum values. This is a readonly array of strings representing all allowed enum values.
 * @param values - The array of values to be filtered. This is an array of unknown type, which may contain values of any type.
 * @returns An array of strings that are present in the enum values array.
 *
 * @example
 * const enumValues = ["apple", "banana", "cherry"] as const;
 * const values = ["apple", "orange", 42, "banana"];
 * const result = filterEnumValues(enumValues, values);
 * console.log(result); // Output: ["apple", "banana"]
 */
export function filterEnumValues<T extends string>(
  enumValues: readonly T[], // An array of allowed enum values, readonly and of type T[]
  values: unknown[], // The array of values to be filtered, of type unknown[]
): T[] {
  return values.filter(
    (value): value is T =>
      // Type guard to ensure the return type is T
      typeof value === "string" && enumValues.includes(value as T), // Ensure the value exists in the enum values array
  );
}
